package com.dxd.onlineshop.controller;
import com.dxd.onlineshop.pojo.NotificationRequest;
import com.dxd.onlineshop.pojo.Notification;
import com.dxd.onlineshop.service.NotificationService;
import com.dxd.onlineshop.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    //推送通知给所有用户
    @PostMapping("/admin/broadcast")
    public ResponseEntity<Void> broadcastNotification(@RequestBody NotificationRequest request) {
        notificationService.createAndBroadcastNotification(request.getContent(), request.getType());
        return ResponseEntity.ok().build();
    }

    //获取到未读通知
    @GetMapping("/unread")
    public ResponseEntity<List<Notification>> getUnreadNotifications(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            Map<String,Object> claims = JwtUtil.parseToken(token);
            Integer userId = (Integer) claims.get("userId");
            List<Notification> notifications = notificationService.getUnreadNotifications(userId);
            return ResponseEntity.ok(notifications);
        } catch(Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(401).build();
        }
    }

    // 新增：获取单个所有通知（包括已读和未读）
    @GetMapping("/all")
    public ResponseEntity<List<Notification>> getAllNotifications(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            Map<String,Object> claims = JwtUtil.parseToken(token);
            Integer userId  =  (Integer) claims.get("userId");
            List<Notification> notifications = notificationService.getAllNotifications(userId);
            return ResponseEntity.ok(notifications);
        }catch(Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }

    }

    //给单个用户推送通知
    @PostMapping
    public ResponseEntity<Notification> createNotification(@RequestBody NotificationRequest request) {
        Notification notification = notificationService.createNotification(request.getUserId(), request.getContent(), request.getType());
        return ResponseEntity.ok(notification);
    }

    //修改通知为已读状态
    @PutMapping("/{id}/read")
    public ResponseEntity<Void> markAsRead(@PathVariable Long id) {
        notificationService.markAsRead(id);
        return ResponseEntity.ok().build();
    }

    //删除通知
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteNotification(@PathVariable Long id) {
        notificationService.deleteNotification(id);
        return ResponseEntity.ok().build();
    }

    //获取单条通知(通过ID)
    @GetMapping("/{id}")
    public ResponseEntity<Notification> getNotification(@PathVariable Long id) {
        Notification notification = notificationService.getNotificationById(id);
        if (notification != null) {
            return ResponseEntity.ok(notification);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}